package com.oo.app.ui.main.activity.girl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.oo.app.R;
import com.oo.app.base.activities.BaseActivity;
import com.oo.app.ui.dialog.LoginSelectSexDialog;
import com.oo.app.ui.main.activity.UserInfoCompileActivity;
import com.oo.app.ui.main.activity.boy.HomeBoyActivity;
import com.oo.app.ui.main.adapter.GrabChatAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import jp.wasabeef.recyclerview.animators.OvershootInLeftAnimator;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

/**
 * @创建者 喻亚龙
 * @描述
 */

public class GrabChatActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;

    @BindView(R.id.tv_mes)
    TextView tvMes;

    private GrabChatAdapter mAdapter;

    List<String> list = new ArrayList<>();

    private int mFirstPageItemCount = 0;

    boolean isMes = true;

    public static void show(Context context) {
        Intent intent = new Intent(context, GrabChatActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_grab_chat;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        initAdapter();

        refresh();

        initAnimation();
    }

    @OnClick({R.id.tv_mes,R.id.ll_scoring_index})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_mes:
                authentication();
                break;
            case R.id.ll_scoring_index:
                ScoringIndexActivity.show(GrabChatActivity.this);
                break;
        }
    }

    public void initAnimation(){
        // 从屏幕底部进入的动画
        TranslateAnimation animation = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f
        );
        animation.setDuration(100);
        tvMes.startAnimation(animation);

        mHandler.postDelayed(rMes, 3000);//延时100毫秒

    }

    private void initAdapter() {
        mRecyclerView.setItemAnimator(new SlideInLeftAnimator());
        mAdapter = new GrabChatAdapter(this, list);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.setItemAnimator(new OvershootInLeftAnimator(1.0f));
        mRecyclerView.getItemAnimator().setAddDuration(500);
        mRecyclerView.getItemAnimator().setRemoveDuration(500);
    }

    @SuppressLint("HandlerLeak")
    Handler h =  new Handler(){
        public void handleMessage(Message msg){
            // call update gui method.
            switch (msg.what) {
                case 1:
                    list.add("1");
//                    setData(true, list);
//                    mAdapter.setEnableLoadMore(true);
                    break;
                case 2:
                    break;
            }
        }
    };


    Handler mHandler = new Handler();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            //do something
//            list.add("1");
//            mAdapter.addData(list);
            mAdapter.add("newly added item", mFirstPageItemCount);
            mFirstPageItemCount++;

            if (mFirstPageItemCount < 4){
                //每隔1s循环执行run方法
                mHandler.postDelayed(this, 100);
            }else if (mFirstPageItemCount == 4){
                //每隔1s循环执行run方法
                mHandler.postDelayed(this, 5000);
            }else {
                mAdapter.clear();
                mFirstPageItemCount = 0;
                //每隔1s循环执行run方法
                mHandler.postDelayed(this, 1000);
            }
        }
    };

    Runnable rMes = new Runnable() {

        @Override
        public void run() {
            //判断是否已经显示出来，显示出来了就淡出，未显示就平移弹出
            if (isMes){
                //do something
                AnimationSet aset=new AnimationSet(true);
                AlphaAnimation aa=new AlphaAnimation(1,0);
                aa.setDuration(2000);
                aset.addAnimation(aa);
                tvMes.startAnimation(aset);
                isMes = false;
                mHandler.postDelayed(this, 2000);
            }else {
                isMes = true;
                initAnimation();
            }
        }
    };

    public void refresh(){
        mAdapter.add("newly added item", mFirstPageItemCount);
        mFirstPageItemCount++;
        if (mFirstPageItemCount > 5){

        }

        mHandler.postDelayed(r, 100);//延时100毫秒

    }

    /**
     * 未认证女主播去认证
     */
    public void authentication(){
        final LoginSelectSexDialog loginSelectSexDialog = new LoginSelectSexDialog(GrabChatActivity.this,"无法抢聊","你还没通过自拍认证","去认证");
        loginSelectSexDialog.show();
        loginSelectSexDialog.setClicklistener(new LoginSelectSexDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                AuthenticationActivity.show(GrabChatActivity.this);
                loginSelectSexDialog.dismiss();
            }

            @Override
            public void doCancel() {
                loginSelectSexDialog.dismiss();
            }
        });
    }

}
